package com.liucxu.qiushimeinv.View.Fragment;

import android.support.design.widget.Snackbar;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.liucxu.qiushimeinv.Base.BaseFragment;
import com.liucxu.qiushimeinv.Consts.Consts;
import com.liucxu.qiushimeinv.Model.Bean.BaseBean;
import com.liucxu.qiushimeinv.Model.Bean.GirlEntity;
import com.liucxu.qiushimeinv.Model.Bean.GirlsBean;
import com.liucxu.qiushimeinv.Presenter.IGirlsPresenter;
import com.liucxu.qiushimeinv.Presenter.Impl.GirlsPresenterImpl;
import com.liucxu.qiushimeinv.R;
import com.liucxu.qiushimeinv.View.Adapter.GirlsAdapter;
import com.liucxu.qiushimeinv.View.IView.IGirlsListView;

import java.util.ArrayList;

/**
 * 姑娘列表的Fragment
 * Created by liukanshan on 2017/1/1.
 */

public class GirlsFragment extends BaseFragment implements IGirlsListView {

    private RelativeLayout no_data;
    private ImageView no_data_iv;
    private ListView content_list;
    private SwipeRefreshLayout swipe_refresh;
    private boolean refresh = true;
    private GirlsAdapter adapter;
    private ArrayList<GirlEntity> girls = new ArrayList<>();
    private boolean stopLoadMore = false;
    private boolean islLoading = false;
    private int page = 1;
    private IGirlsPresenter prsenter;

    @Override
    public int getInflateLayout() {
        return R.layout.fragment_girls;
    }

    @Override
    public void setupView() {
        no_data = $(R.id.no_data);
        no_data_iv = $(R.id.no_data_iv);
        content_list = $(R.id.content_list);
        swipe_refresh = $(R.id.swipe_refresh);
        swipe_refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh = true;
                getGirlsByPage();
            }
        });
        adapter = new GirlsAdapter(mContext, girls);
        content_list.setAdapter(adapter);
        prsenter = new GirlsPresenterImpl(this);
        content_list.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView absListView, int i) {

            }

            @Override
            public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                int lastVisibleItem = absListView.getLastVisiblePosition();
                if (lastVisibleItem >= 0) {
                    if (!stopLoadMore && !islLoading) {
                        if(lastVisibleItem+1>=totalItemCount){
                            refresh = false;
                            getGirlsByPage();
                        }
                    }
                }
            }
        });
    }

    @Override
    public void setupData() {
        refresh = true;
        getGirlsByPage();
    }

    @Override
    public void showLoading() {
        if (refresh) {
            swipe_refresh.setRefreshing(true);
        }

    }

    @Override
    public void hideLoading() {
        swipe_refresh.setRefreshing(false);
    }

    @Override
    public void showError(BaseBean bean) {
        stopLoadMore = false;
        islLoading = false;
        Snackbar.make(swipe_refresh, bean.getDesc(), Snackbar.LENGTH_SHORT).show();
    }

    @Override
    public void setGirls(GirlsBean bean) {
        if (bean.getCode() == Consts.REQUEST_SUCCESS) {
            if (refresh) {
                girls.clear();
                page = 1;
            }
            page++;
            girls.addAll(bean.getList());
            stopLoadMore = false;
            islLoading = false;
            adapter.setDatas(girls);
            if (girls.size() > 0) {
                no_data.setVisibility(View.GONE);
                swipe_refresh.setVisibility(View.VISIBLE);
            } else {
                if(refresh){
                    no_data.setVisibility(View.VISIBLE);
                    swipe_refresh.setVisibility(View.GONE);
                }
                stopLoadMore=false;
                islLoading=false;
            }
        }
    }

    private void getGirlsByPage() {
        if (!refresh) {
            if (stopLoadMore) {
                return;
            } else {
                stopLoadMore = true;
                islLoading = true;
            }
        } else {
            stopLoadMore = true;
            islLoading = true;
        }
        int current = page;
        if (refresh) {
            current = 1;
        } else {
            current = page;
        }
        prsenter.getGirls(current + "");
    }
}
